<template>
  <div class="breadcrumb-container" :class="{ 'breadcrumb-loading': isRouteChanging }">
    <el-breadcrumb separator="" class="custom-breadcrumb">
      <el-breadcrumb-item v-for="(item, index) in breadcrumbItems" :key="index" :class="{
        'breadcrumb-clickable': item.clickable,
        'breadcrumb-current': !item.clickable && index === breadcrumbItems.length - 1
      }" @click="handleBreadcrumbClick(item)">
        <div class="breadcrumb-item-content">
          <el-icon v-if="index === 0" class="home-icon">
            <House />
          </el-icon>
          <span class="breadcrumb-text">{{ item.title }}</span>
          <el-icon v-if="index < breadcrumbItems.length - 1" class="separator-icon">
            <ArrowRight />
          </el-icon>
        </div>
      </el-breadcrumb-item>
    </el-breadcrumb>
  </div>
</template>

<script setup>
import { computed } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { House, ArrowRight } from '@element-plus/icons-vue'
import { useMetaStore } from '@/stores'

// Props
const props = defineProps({
  isRouteChanging: {
    type: Boolean,
    default: false
  }
})

const route = useRoute()
const router = useRouter()
const metaStore = useMetaStore()

// 计算属性 - 基于pinia meta信息的动态面包屑
const breadcrumbItems = computed(() => {
  const metas = metaStore.getMeta()
  if (metas && metas.length > 0) {
    return metas.map((item, index) => ({
      title: item.name,
      path: index === 0 ? item.path : null,
      clickable: index === 0 && item.path !== route.path
    }))
  }

  // 如果没有meta信息，返回默认首页
  return [{ title: '首页', path: '/dashboard', clickable: true }]
})

// 面包屑点击处理
const handleBreadcrumbClick = (item) => {
  if (item.clickable && item.path && item.path !== route.path) {
    router.push(item.path)
  }
}
</script>

<style scoped lang="scss">
/* 面包屑样式 */
.breadcrumb-container {
  transition: opacity 0.3s ease;
  padding-bottom: 0.5rem;
  margin-bottom: 1rem;
}

.breadcrumb-loading {
  opacity: 0.6;
}

.breadcrumb-loading .breadcrumb-item-content {
  animation: breadcrumb-pulse 1.5s ease-in-out infinite;
}

@keyframes breadcrumb-pulse {

  0%,
  100% {
    opacity: 1;
  }

  50% {
    opacity: 0.5;
  }
}

.custom-breadcrumb {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 0;
}

.custom-breadcrumb :deep(.el-breadcrumb__item) {
  display: flex;
  align-items: center;
  font-size: 0;
}

.breadcrumb-item-content {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 4px 8px;
  border-radius: 6px;
  transition: all 0.3s ease;
  position: relative;
}

.breadcrumb-text {
  font-size: 13px;
  color: #64748b;
  font-weight: 500;
  white-space: nowrap;
  transition: color 0.3s ease;
}

.home-icon {
  font-size: 14px;
  color: #64748b;
  transition: color 0.3s ease;
}

.separator-icon {
  font-size: 12px;
  color: #cbd5e1;
  margin-left: 2px;
  transition: color 0.3s ease;
}

.breadcrumb-clickable .breadcrumb-item-content {
  cursor: pointer;
}

.breadcrumb-clickable .breadcrumb-item-content:hover {
  background-color: #f1f5f9;
  transform: translateY(-1px);
  box-shadow: 0 2px 8px rgba(59, 130, 246, 0.15);
}

.breadcrumb-clickable .breadcrumb-item-content:active {
  transform: translateY(0);
  transition: transform 0.1s ease;
}

.breadcrumb-clickable .breadcrumb-item-content:hover .breadcrumb-text,
.breadcrumb-clickable .breadcrumb-item-content:hover .home-icon {
  color: #3b82f6;
}

.breadcrumb-clickable .breadcrumb-item-content:hover .separator-icon {
  color: #94a3b8;
}

.breadcrumb-current .breadcrumb-text {
  color: #1e293b;
  font-weight: 600;
  background: linear-gradient(135deg, #3b82f6, #8b5cf6);
  background-clip: text;
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
}

.breadcrumb-current .home-icon {
  color: #3b82f6;
}

/* 删除面包屑的下划线动画 */
.breadcrumb-item-content::before {
  display: none;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .breadcrumb-text {
    font-size: 12px;
  }

  .breadcrumb-item-content {
    padding: 3px 6px;
    gap: 4px;
  }
}
</style>
